package com.project.weimi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.project.weimi.R;
import com.project.weimi.adapter.IntegralRecordAdapter;
import com.project.weimi.application.Constant;
import com.project.weimi.entity.response.IntegralRecordEntity;
import com.project.weimi.entity.response.IntegralRecordEntity.IntegralRecord;
import com.project.weimi.interfaces.MyCallBack;
import com.project.weimi.utils.GsonUtil;
import com.project.weimi.utils.NetUtils;
import com.project.weimi.utils.SetClickListener;
import com.project.weimi.utils.XUtil;
import com.project.weimi.widget.ToastMessage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * 我的积分Activity
 * @author Administrator
 *
 */
public class MyIntegralActivity extends BaseActivity implements OnRefreshListener {

	private TextView tv_integralExplain,tv_integralNum,tv_nocontent;
	private Button btn_exchangeGoods;
	private SwipeRefreshLayout swipeRefresh;
	private RecyclerView recyclerview;
	private LinearLayoutManager layoutManager;
	private int lastVisibleItem;
	
	private List<IntegralRecord> list = new ArrayList<IntegralRecord>();
	private IntegralRecordAdapter adapter;
	
	private String totalIntegral;
	private String userPhone;
	int pageIndex;
	int itemNum;
	
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			
			switch (msg.what) {
			case 0:
				if(!NetUtils.isConnected(MyIntegralActivity.this)){
					ToastMessage.showToast(MyIntegralActivity.this, "网络不给力，请检查网络设置");
				} else {
					swipeRefresh.setRefreshing(false);
					list.clear();
					pageIndex = 1;
					initHttp(pageIndex,10);
					ToastMessage.showMyToast(MyIntegralActivity.this, "刷新完成");
				}
				
				break;
			case 1:
				pageIndex += 1;
				initHttp(pageIndex,10);
				adapter.notifyDataSetChanged();
				break;
			}
		};
	};
	
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentViewWithBar(R.layout.activity_myintegral, "我的积分");
		
		initView();
		
		if(!NetUtils.isConnected(this)){
			ToastMessage.showToast(this, "网络不给力，请检查网络设置");
		} else {
			pageIndex = 1;
			initHttp(pageIndex,10);
		}
		
	}
	
	private void initView(){
		tv_integralExplain = (TextView)findViewById(R.id.tv_integralExplain);
		tv_integralNum = (TextView)findViewById(R.id.tv_integralNum);
		tv_nocontent = (TextView)findViewById(R.id.tv_nocontent);
		btn_exchangeGoods = (Button)findViewById(R.id.btn_exchangeGoods);
		swipeRefresh = (SwipeRefreshLayout)findViewById(R.id.swipeRefresh);
		recyclerview = (RecyclerView)findViewById(R.id.recyclerview);
		
		swipeRefresh.setColorScheme(R.color.color1,R.color.color2,R.color.color3,R.color.color4);
		swipeRefresh.setOnRefreshListener(this);
		
		recyclerview.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
				super.onScrollStateChanged(recyclerView, newState);
				
				//Log.e("TAG", "加载更多===前---"+adapter.getItemCount());
				if(itemNum < 10){
					ToastMessage.showMyToast(MyIntegralActivity.this, "暂无更多数据");
					return;
				}
				
				if(newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()){
					handler.sendEmptyMessageDelayed(1, 3000);
				}
				
			}
			
			@Override
			public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
				super.onScrolled(recyclerView, dx, dy);
				lastVisibleItem = layoutManager.findLastVisibleItemPosition();
			}
		});
		
		recyclerview.setHasFixedSize(true);
		layoutManager = new LinearLayoutManager(MyIntegralActivity.this);
		recyclerview.setLayoutManager(layoutManager);
		recyclerview.setItemAnimator(new DefaultItemAnimator());
		
		// 监听
		SetClickListener.setClickListener(listener, tv_integralExplain,btn_exchangeGoods);
		
	}
	
	private OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			Intent intent = null;
			switch (v.getId()) {
			case R.id.tv_integralExplain: // 跳转到“积分说明”页面
				intent = new Intent(MyIntegralActivity.this,IntegralExplainActivity.class);
				break;
			case R.id.btn_exchangeGoods: // 跳转到“首页”
				intent = new Intent(MyIntegralActivity.this,MainActivity.class);
				intent.putExtra("fragId", 0);
				break;
			default:
				break;
			}
			startActivity(intent);
		}
	};
	
	private void initHttp(final int pageIndex,int pageSize){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", userPhone);//  "13462467089" 
		map.put("pageIndex", pageIndex);
		map.put("pageSize", pageSize);
		
		XUtil.post(Constant.INTEGAL_RECORD, map, new MyCallBack<String>(){
			
			int pos;
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				if(null != result && !"".equals(result)){
					//Log.e("TAG", "MyIntegralActivity===result---="+result);
					IntegralRecordEntity entity = (IntegralRecordEntity) GsonUtil.jsonToBean(
							result, IntegralRecordEntity.class);
					if(null != entity){
						
						if(null != entity.result){
							//Log.e("TAG", "MyIntegralActivity===list.size前---="+list.size());
							pos = list.size();
							
							if(pageIndex > 1){
								list.addAll(entity.result);
							} else {
								list = entity.result;
								if(list.size() > 0){
									tv_nocontent.setVisibility(View.GONE);
									swipeRefresh.setVisibility(View.VISIBLE);
								} else {
									tv_nocontent.setVisibility(View.VISIBLE);
									swipeRefresh.setVisibility(View.GONE);
								}
							}
						}
							
						//Log.e("TAG", "MyIntegralActivity===list.size后---="+list.size());
						itemNum = list.size() - pos;	
						//Log.e("TAG", "MyIntegralActivity===请求数量---="+itemNum);
						
						adapter = new IntegralRecordAdapter(MyIntegralActivity.this,list);
						recyclerview.scrollToPosition(pos);
						recyclerview.setAdapter(adapter);
						
					}
				}
			}
			
			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				super.onError(ex, isOnCallback);
			}
		});
	}
	
	@Override
	public void onRefresh() {
		handler.sendEmptyMessageDelayed(0, 3000);
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		if(TextUtils.isEmpty(memberId)){
			ToastMessage.showMyToast(MyIntegralActivity.this, "登录后才能查看积分记录");
			return;
		} else {
			totalIntegral = integrate;
			userPhone = memberId;
			//Log.e("TAG", "MyIntegralActivity===="+integrate);
			//Log.e("TAG", "MyIntegralActivity===="+memberId);
			tv_integralNum.setText(totalIntegral);
		}
	}
	
	@Override
	void setStatusBar(int statusBarColorRes) {
		super.setStatusBar(R.color.transparent_3);
	}
	
	
}
